#include "mainwindow.h"

MainWindow::MainWindow(const QString& title, QWidget *parent) : QMainWindow(parent)
{
	setWindowTitle(title);

    fileMenu = menuBar()->addMenu("&File");
    helpMenu = menuBar()->addMenu("&Help");

    openAction = fileMenu->addAction("&Open");
    quitAction = fileMenu->addAction("&Quit");
    infoAction = helpMenu->addAction("&Info");

	connect(openAction, SIGNAL(triggered()), this, SLOT(on_menu_open()));
	connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));
	connect(infoAction, SIGNAL(triggered()), this, SLOT(on_menu_info()));

    centralWidget = new QWidget();
    mainLayout = new QVBoxLayout();
    ctrlLayout = new QHBoxLayout();
	centralWidget->setLayout(mainLayout);

    detector = new Detector("haarcascades/haarcascade_frontalface_alt.xml");

	detector->setScale(Detector::DEFAULT_SCALE);
    camera = new Camera(0);

	camview = new CamView();
	camview->setDetector(detector);

	worker = new Worker(camera, camview);

	int width = camera->get(CV_CAP_PROP_FRAME_WIDTH);
	int height = camera->get(CV_CAP_PROP_FRAME_HEIGHT);
	camview->setFixedSize(width, height);
	camview->updateGeometry();

    label = new QLabel("Scale: ");

    slider = new QSlider(Qt::Horizontal);
	slider->setTickInterval(1);
	slider->setMinimum(0);
	slider->setMaximum(10*Detector::MAX_SCALE);
	slider->setSliderPosition(10*Detector::DEFAULT_SCALE);
	slider->setTickPosition(QSlider::TicksBothSides);
	connect(slider, SIGNAL(sliderMoved(int)), this, SLOT(on_slider_moved(int)));

    captureButton = new QPushButton("&Capture");
    connect(captureButton, SIGNAL(clicked()), this, SLOT(on_capture_button_clicked()));

    ctrlLayout->addWidget(label);
	ctrlLayout->addWidget(slider);
	ctrlLayout->addStretch();
    ctrlLayout->addWidget(captureButton);

	mainLayout->addWidget(camview);
	mainLayout->addLayout(ctrlLayout);
	setCentralWidget(centralWidget);

	worker->start();
}

void MainWindow::closeEvent(QCloseEvent *event)
{
	if (worker->isRunning()) {
		worker->terminate();

		while (!worker->isFinished()) {
			QThread::msleep(10);
		}
	}
}

void MainWindow::on_capture_button_clicked()
{
	cv::Mat frame = camera->getNextFrame();
	QDateTime datetime = QDateTime::currentDateTime();
    cv::imwrite(datetime.toString("yyyyMMdd-hhmmss").toStdString() + ".png", frame);
}

void MainWindow::on_menu_open()
{
	QString filename = QFileDialog::getOpenFileName(0, "Choose file", QString(), "Image Files (*.png *.jpg *.bmp)");
	std::vector<cv::Rect> objects;
	cv::Mat image = cv::imread(filename.toStdString());
	
	double oldScale = detector->getScale();
	detector->setScale(0.5);
	detector->detect(image, objects);
	detector->setScale(oldScale);

	for (std::vector<cv::Rect>::iterator it = objects.begin(); it != objects.end(); ++it) {
		cv::rectangle(image, *it, cv::Scalar(0, 0, 255), 2);
	}

    cv::Size newSize = image.size();
    double width = (double)image.size().width;
    double height = (double)image.size().height;
    double scale = width/height;

    const int max_width = 800;
    const int max_height = 600;

    if((width/max_width) > (height/max_height)) {
        newSize.width = max_width;
        newSize.height = newSize.width/scale;
    } else {
        newSize.height = max_height;
        newSize.width = newSize.height*scale;
    }

    cv::resize(image, image, newSize);

    if(imageView == NULL) {
        imageView = new CamView();
    }

    imageView->setFixedSize(image.size().width, image.size().height);
    imageView->newFrame(image);
    imageView->updateGeometry();

    if(!imageView->isVisible()) {
        imageView->setVisible(true);
    }
}

void MainWindow::on_menu_info()
{
    QMessageBox::information(0, "HCMUTE", "Facedetect using OpenCV\n\nTu Quoc Lan - 11110066\nDang Quang Hung - 11110058");
}

void MainWindow::on_slider_moved(int value)
{
	detector->setScale(1.0+value/10);
}

MainWindow::~MainWindow()
{
}
